package com.ys.http.network;

import com.ys.http.body.DownResponseBody;
import com.ys.http.download.OnDownloadListener;

import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;

/*
* 下载拦截器
* */
public class DownloadInterceptor implements Interceptor {

    private final DownResponseBody mDownResponseBody;

    public DownloadInterceptor(OnDownloadListener listener) {
        mDownResponseBody = new DownResponseBody(listener);
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        mDownResponseBody.setResponseBody(response.body());
        return response.newBuilder()
                .body(mDownResponseBody)
                .build();
    }

    public void dispose() {
        mDownResponseBody.dispose();
    }
}
